package com.xiaoxing.zhinenghuacangku_linyi.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.dds.soap.SoapListener;
import com.dds.soap.SoapParams;
import com.dds.soap.SoapUtil;
import com.jess.arms.base.ArmsBaseConstants;
import com.jess.arms.base.BaseConstants;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.SharedPreferencesHelper;
import com.xiaoxing.gifloadingview.LoadingDialogUtil;
import com.xiaoxing.zhinenghuacangku_linyi.R;
import com.xiaoxing.zhinenghuacangku_linyi.mvp.model.entity.XinyemianBean;
import com.xiaoxing.zhinenghuacangku_linyi.mvp.ui.entity.XinyemianhuanchongweiBean;

import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;

import java.util.ArrayList;
import java.util.List;

import me.jessyan.armscomponent.commonres.view.NiceSpinner;

import static com.jess.arms.base.BaseConstants.SCAN_REQUEST_CODE;

public class ZhijianfinishActivity extends AppCompatActivity {

    private ImageView iv_saoma;
    private EditText et_gong_ying_shang,et_remark;
    private TextView tv_cha_xun,tv_huanchongwei;
    private NiceSpinner nice_storage,nice_hcw;
    private Button btn_commit_ruku_agv,btn_commit_ruku_liku;
    protected SharedPreferencesHelper mSharedPreferencesHelper;
    String url="";
    String id="";
    String cangWeiPosition="";
    int spid=0;
    String spname="";
    String mSptname="";
    String ids="";
    String mScanMid="";
    String mid="";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);

        iv_saoma=findViewById(R.id.iv_saoma);
        et_gong_ying_shang=findViewById(R.id.et_gong_ying_shang);
        tv_cha_xun=findViewById(R.id.tv_cha_xun);
        tv_huanchongwei=findViewById(R.id.tv_huanchongwei);
        nice_storage=findViewById(R.id.nice_storage);
        btn_commit_ruku_agv=findViewById(R.id.btn_commit_ruku_agv);
        btn_commit_ruku_liku=findViewById(R.id.btn_commit_ruku_liku);
        nice_hcw=findViewById(R.id.nice_hcw);
        et_remark=findViewById(R.id.et_remark);
        Toolbar toolbar = findViewById(R.id.toolbar);
        TextView tvTitle = findViewById(R.id.title);
        tvTitle.setText("质检完成入库");
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    finish();
            }
        });
        iv_saoma.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent newIntent = new Intent(ZhijianfinishActivity.this, LinYi_ScanQRCodeActivity.class);
                mSharedPreferencesHelper.putInt(SCAN_REQUEST_CODE, 102);
                // 开始一个新的 Activity等候返回结果
                startActivityForResult(newIntent, 102);
            }
        });
//        mSharedPreferencesHelper.getString(BaseConstants.API_USER_NAME);
//        mSharedPreferencesHelper.getString(BaseConstants.API_PWD);
        mSharedPreferencesHelper = SharedPreferencesHelper.getInstance(this);
        url="http://" + mSharedPreferencesHelper.getString(ArmsBaseConstants.IP) + ":"
                + mSharedPreferencesHelper.getString(ArmsBaseConstants.PORT)
                + "/StoreService.asmx";

        tv_cha_xun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String methodName = "GetUserSpaceListForAutoStore";
                SoapParams params = null;
                params = new SoapParams();
                params.put("user_id",mSharedPreferencesHelper.getString(BaseConstants.UID));
                params.put("spname",et_gong_ying_shang.getText().toString().trim());
                SoapUtil.getInstance(ZhijianfinishActivity.this).callEntry(url, BaseConstants.NAME_SPACE, methodName, params, new SoapListener() {
                    @Override
                    public void onSuccess(int statusCode, SoapObject object) {
                        LoadingDialogUtil.dismissDialog();

                        String result = object.getProperty(methodName + "Result").toString();
                        if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100")) {
                            try {

                                List<XinyemianBean> getUserSpaceListForAutoStoreList = ArmsUtils.parseString2List(result, XinyemianBean.class);
                                initSpPosition(getUserSpaceListForAutoStoreList);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                        } else {

                        }
                    }

                    @Override
                    public void onFailure(int statusCode, String content, Throwable error) {

                    }

                    @Override
                    public void onFailure(int statusCode, SoapFault fault) {

                    }
                },mSharedPreferencesHelper.getString(BaseConstants.API_USER_NAME), mSharedPreferencesHelper.getString(BaseConstants.API_PWD));
            }
        });






                String methodNames = "GetBatchFreeInstoreBuffer";
                SoapParams paramss = null;
                paramss = new SoapParams();
                SoapUtil.getInstance(ZhijianfinishActivity.this).callEntry(url, BaseConstants.NAME_SPACE, methodNames, paramss, new SoapListener() {
                    @Override
                    public void onSuccess(int statusCode, SoapObject object) {
                        LoadingDialogUtil.dismissDialog();

                        String result = object.getProperty(methodNames + "Result").toString();
                        if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100")) {
                            try {

                                List<XinyemianhuanchongweiBean> getUserSpaceListForAutoStoreList = ArmsUtils.parseString2List(result, XinyemianhuanchongweiBean.class);
                                inHuanChong(getUserSpaceListForAutoStoreList);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                        } else {

                        }
                    }

                    @Override
                    public void onFailure(int statusCode, String content, Throwable error) {

                    }

                    @Override
                    public void onFailure(int statusCode, SoapFault fault) {

                    }
                },mSharedPreferencesHelper.getString(BaseConstants.API_USER_NAME), mSharedPreferencesHelper.getString(BaseConstants.API_PWD));

        btn_commit_ruku_agv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String methodNames = "InstoreCallAGVFromQualityZone";
                SoapParams paramss = null;
                paramss = new SoapParams();
                paramss.put("mid",mScanMid);
                paramss.put("instoreBufferId",ids);
                paramss.put("spid",String.valueOf(spid));
                paramss.put("uid",mSharedPreferencesHelper.getString(BaseConstants.UID));
                paramss.put("remark",et_remark.getText().toString().trim());
                Log.e("8888888888======1======",mScanMid);
                Log.e("8888888888=======2=====",ids);
                Log.e("8888888888======3======",String.valueOf(spid));
                if (spid>0){
                    SoapUtil.getInstance(ZhijianfinishActivity.this).callEntry(url, BaseConstants.NAME_SPACE, methodNames, paramss, new SoapListener() {
                        @Override
                        public void onSuccess(int statusCode, SoapObject object) {
                            LoadingDialogUtil.dismissDialog();

                            String result = object.getProperty(methodNames + "Result").toString();
//                        if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100")){
                            int a=Integer.parseInt(result);
                            if (a>0){
                                Toast.makeText(ZhijianfinishActivity.this,"发送成功",Toast.LENGTH_LONG).show();
                            }else {
                                Toast.makeText(ZhijianfinishActivity.this,"入库失败",Toast.LENGTH_LONG).show();
                            }
//                        }

                        }

                        @Override
                        public void onFailure(int statusCode, String content, Throwable error) {

                        }

                        @Override
                        public void onFailure(int statusCode, SoapFault fault) {

                        }
                    },mSharedPreferencesHelper.getString(BaseConstants.API_USER_NAME), mSharedPreferencesHelper.getString(BaseConstants.API_PWD));
                }else {
                    Toast.makeText(ZhijianfinishActivity.this,"请点击查询按钮选择入库仓位！",Toast.LENGTH_LONG).show();
                }

            }
        });
        btn_commit_ruku_liku.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String methodNames = "InstoreCallliKu";
                SoapParams paramss = null;
                paramss = new SoapParams();
                paramss.put("mid",mScanMid);
                paramss.put("spid",String.valueOf(spid));
                paramss.put("uid",mSharedPreferencesHelper.getString(BaseConstants.UID));
                paramss.put("num","0");
                paramss.put("remark",et_remark.getText().toString().trim());
                SoapUtil.getInstance(ZhijianfinishActivity.this).callEntry(url, BaseConstants.NAME_SPACE, methodNames, paramss, new SoapListener() {
                    @Override
                    public void onSuccess(int statusCode, SoapObject object) {
                        LoadingDialogUtil.dismissDialog();

                        String result = object.getProperty(methodNames + "Result").toString();
                        if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100")){
                            int a=Integer.parseInt(result);
                            if (a>0){
                                Toast.makeText(ZhijianfinishActivity.this,"发送成功",Toast.LENGTH_LONG).show();
                            }
                        }
                    }

                    @Override
                    public void onFailure(int statusCode, String content, Throwable error) {

                    }

                    @Override
                    public void onFailure(int statusCode, SoapFault fault) {

                    }
                },mSharedPreferencesHelper.getString(BaseConstants.API_USER_NAME), mSharedPreferencesHelper.getString(BaseConstants.API_PWD));
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 102){
            Bundle extras = data.getExtras();
            mid = extras.getString("scan_result");
            mScanMid = mid;
        }
    }

    private void initSpPosition(List<XinyemianBean> getUserSpaceListForAutoStoreList) {
        ArrayList<String> list = new ArrayList<String>();
        for (int i = 0; i < getUserSpaceListForAutoStoreList.size(); i++) {
            list.add(String.valueOf(
                    new StringBuilder()
                            .append(getUserSpaceListForAutoStoreList.get(i).getSpname())
            ));
        }
        nice_storage.attachDataSource(list);
        spid=getUserSpaceListForAutoStoreList.get(0).getSpid();
        nice_storage.addOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                cangWeiPosition = String.valueOf(getUserSpaceListForAutoStoreList.get(position).getSpid());
                spid = getUserSpaceListForAutoStoreList.get(position).getSpid();
                spname = getUserSpaceListForAutoStoreList.get(position).getSpname();
                mSptname = getUserSpaceListForAutoStoreList.get(position).getSptname();


            }
        });
    }
    private void inHuanChong(List<XinyemianhuanchongweiBean> xinyemianhuanchongweiBeans){
        ArrayList<String> list = new ArrayList<String>();
        for (int i = 0; i < xinyemianhuanchongweiBeans.size(); i++) {
            list.add(String.valueOf(
                    new StringBuilder()
                            .append(xinyemianhuanchongweiBeans.get(i).getBuffer_name())
            ));
        }
        nice_hcw.attachDataSource(list);
        ids=xinyemianhuanchongweiBeans.get(0).getId();
        nice_hcw.addOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ids = xinyemianhuanchongweiBeans.get(position).getId();
            }
        });
    }
}
